package com.winning.vsts.merge;

import com.alibaba.fastjson.JSONObject;
import com.winning.vsts.merge.service.configuration.VstsContext;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.service.VstsClient;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 自动取消PR
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/19
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class AutoCancelPullRequestApplication implements ApplicationRunner {

    private final static Logger log = LoggerFactory.getLogger(AutoMergeApplication.class);

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private VstsContext vstsContext;

    @Autowired
    private VstsClient vstsClient;

    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .main(AutoCancelPullRequestApplication.class)
            .sources(AutoCancelPullRequestApplication.class)
            .web(WebApplicationType.NONE)
            .build().run(args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        String targetBranch = "RC";
        String sourceBranch = "develop";
        log.info("====================开始取消分支*{}*合并分支{}的PR===================", sourceBranch, targetBranch);
        List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject(null, null);
        for (VstsProject vstsProject : vstsProjects) {
            try{
                RestTemplate restTemplate = vstsContext.restTemplate(vstsProject);
                List<JSONObject> prResults = vstsClient.queryBranchPullRequest(restTemplate, vstsProject, targetBranch);
                for (JSONObject prResult : prResults) {
                    String createUser = prResult.getJSONObject("createdBy").getString("displayName");
                    String projectName = prResult.getJSONObject("repository").getString("name");
//                    if (!createUser.contains("ylk")) {
                        log.warn("{}创建项目{}的{}请求未进行合并成功", createUser, projectName, targetBranch);
//                    } else {
//                        // 铿爷的话采用项目管理员
//                        List<VstsProject> projects = vstsProjectService.queryVstsProject(projectName, null);
//                        String projectOwner = projects.get(0).getProjectOwner();
//                        log.warn("{}管理的项目{}的{}请求未进行合并成功", projectOwner, projectName, targetBranch);
//                    }
                    String sourceRefName = prResult.getString("sourceRefName");
                    if (!sourceRefName.contains(sourceBranch)) {
                        continue;
                    }
                    String pullRequestId = prResult.getString("pullRequestId");

                    vstsClient.abandoned(restTemplate, vstsProject, pullRequestId);

                }
            }
            catch (Exception e) {
                log.warn("项目{}的{}请求未进行撤销成功******************",  vstsProject.getProjectName(), targetBranch);
            }
        }

        log.info("====================取消分支*{}*合并分支{}的PR完成===================", sourceBranch, targetBranch);
    }
}
